﻿Public Class FormOrdenCompraListado

    Dim classController As New Class_RC_Controller.rc_OrdenCompra()
    Dim fact As New Class_RC_Controller.rc_Facturas()
    Dim pro As New Class_RC_Controller.rc_Proveedores
    Dim cli As New Class_RC_Controller.rc_Clientes

    Private p_Datos As DataTable
    Public Property datos_Reporte() As DataTable
        Get
            Return p_Datos
        End Get
        Set(value As DataTable)
            p_Datos = value
        End Set
    End Property

    Private m_frmOrdenCompra As FrmOrdenCompra
    Public Property frmOrdenCompra() As FrmOrdenCompra
        Get
            Return m_frmOrdenCompra
        End Get
        Set(value As FrmOrdenCompra)
            m_frmOrdenCompra = value
        End Set
    End Property

    Private Sub FormOrdenCompraListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgOrdenes.AutoGenerateColumns = False
        fechaInicio.Value = DateTime.Now.AddDays(-5)

        CargarCombos()
        CargarOrdenes()

    End Sub

    Private Sub CargarOrdenes()

        Dim codResponsable As Integer
        If cbxResponsable.Checked Then
            codResponsable = cboResponsable.SelectedValue
        End If

        Dim estadoVenta As String = ""
        If cbxEstado.Checked Then
            estadoVenta = cboEstado.Text
        End If

        Dim dt As New DataTable
        dt = classController.get_OrdenCompra_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, 0, txtRuc.Text, txtNro.Text,
                                                 txtNombre.Text, codResponsable, estadoVenta)

        datos_Reporte = classController.get_OrdenCompra_Impresion(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, 0, txtRuc.Text, txtNro.Text,
                                                 txtNombre.Text, codResponsable, estadoVenta)
        dgOrdenes.DataSource = dt

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub CargarCombos()

        Dim dsVend As DataTable
        dsVend = cli.get_vendedores(oiml.dbConn)
        With Me.cboResponsable
            .DataSource = dsVend
            .DisplayMember = "usuario"
            .ValueMember = "codUsuario"
        End With

        cboEstado.SelectedIndex = 0
        cboResponsable.SelectedIndex = 0

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgOrdenes.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgOrdenes.SelectedRows(0).Cells(0).Value

                If frmOrdenCompra Is Nothing Then

                    frmOrdenCompra = New FrmOrdenCompra()
                    frmOrdenCompra.codOrden = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmOrdenCompra.ShowDialog()
                    CargarOrdenes()
                    frmOrdenCompra.Dispose()
                    frmOrdenCompra = Nothing
                    'End If
                Else
                    frmOrdenCompra.codOrden = cod
                    frmOrdenCompra.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarOrdenes()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarOrdenes()

        End If
    End Sub

    Private Sub dgOrdenes_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgOrdenes.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgOrdenes_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgOrdenes.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub



    Private Sub txtRuc_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtRuc.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarOrdenes()

        End If
    End Sub

    Private Sub txtNro_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNro.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarOrdenes()

        End If
    End Sub

    Private Sub cbxResponsable_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxResponsable.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarOrdenes()

        End If
    End Sub

    Private Sub cboResponsable_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboResponsable.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarOrdenes()

        End If
    End Sub

    Private Sub cbxEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarOrdenes()

        End If
    End Sub

    Private Sub cboEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarOrdenes()

        End If
    End Sub

    Private m_frmRpts As FormReportes
    Public Property frmRpts() As FormReportes
        Get
            Return m_frmRpts
        End Get
        Set(value As FormReportes)
            m_frmRpts = value
        End Set
    End Property

    Private Sub btnImprimir_Click(sender As System.Object, e As System.EventArgs) Handles btnImprimir.Click
        If frmRpts Is Nothing Then
            frmRpts = New FormReportes()
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "ORDENES"
            frmRpts.datos_Reporte = datos_Reporte
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New FormReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "ORDENES"
                frmRpts.datos_Reporte = datos_Reporte
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "ORDENES"
                frmRpts.datos_Reporte = datos_Reporte
                frmRpts.Select()
                frmRpts.Activate()
            End If
        End If
    End Sub
End Class